package com.linlang.grasharepro.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.adapter.BiYeShareMessageAdapter;
import com.linlang.grasharepro.dao.CollectionDB;
import com.linlang.grasharepro.dao.NewMessageDB;
import com.linlang.grasharepro.dao.ShareMessageDB;
import com.linlang.grasharepro.model.CollectionDomain;
import com.linlang.grasharepro.model.NewMessageDomain;
import com.linlang.grasharepro.model.ShareCoenten;
import com.linlang.grasharepro.model.ShareMessage;
import com.linlang.grasharepro.utils.FileUtiles;
import com.linlang.grasharepro.utils.ListViewUitls;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 店铺详情模块
 */
public class ShareDetailsActivity extends Activity {

    private ShareCoenten info = null;
    private TextView userName, time, profe, title, content;
    private ImageView head, type,iv_collect;
    //留言List
    private List<ShareMessage> message_list = new ArrayList<ShareMessage>();

    private ListView messageList;
    private ShareMessageDB shareMessageDB;
    private NewMessageDB newMessageDB;
    private CollectionDB collectionDB;
    BiYeShareMessageAdapter shareAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_share_details);

        // 获取从列表当中传递过来的数据
        Intent intent = getIntent();
        Bundle bund = intent.getExtras();
        info = (ShareCoenten) bund.getSerializable("shareContent");
        initView();
        initShareMessage();
    }

    /**
     * 初始化分享信息
     */
    public void initShareMessage() {

        shareMessageDB = new ShareMessageDB(this);
        shareMessageDB.open();
        message_list = shareMessageDB.queryByShareCoentenId(info.get_id());

        this.messageList = (ListView) this.findViewById(R.id.detail_messageList);
        shareAdapter = new BiYeShareMessageAdapter(message_list, this);
        messageList.setAdapter(shareAdapter);
        ListViewUitls.setListViewHeightBasedOnChildren(messageList);
        //设置单击事件
        messageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ShareDetailsActivity.this, message_list.get(position).toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 点击用户信息区进入个人详细信息页
     *
     * @param view
     */
    public void userDetail(View view) {

        Bundle bundle = new Bundle();
        bundle.putSerializable("shareContent", info);
        Intent intent = new Intent(ShareDetailsActivity.this,
                UserInfoActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 点击留言发送按钮
     */
    public void leaveWord(String inputName) {
        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");
        String type = preferences.getString("type", "");
        String major = preferences.getString("major", "");
        String headUrl = preferences.getString("headUrl", "");

        shareMessageDB = new ShareMessageDB(this);
        shareMessageDB.open();

        ShareMessage shareMessage = new ShareMessage();
        shareMessage.setUserName(account);
        shareMessage.setShareCoentenId(info.get_id());
        shareMessage.setContent(inputName);
        shareMessage.setType(type);
        shareMessage.setHeadUrl(headUrl);

        String dateStr = "";
        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            dateStr = simpleDateFormat.format(date);
            shareMessage.setTime(dateStr);
        } catch (Exception e) {

        }

        //新增留言表
        int strid = shareMessageDB.insertShareMessage(shareMessage);

        //新增新消息表
        NewMessageDomain newMessageDomain = new NewMessageDomain();
        newMessageDomain.setType("2");
        newMessageDomain.setTime(dateStr);
        newMessageDomain.setFromUser(account);
        newMessageDomain.setToUser(info.getUserName());
        newMessageDomain.setContent(inputName);

        newMessageDB = new NewMessageDB(this);
        newMessageDB.open();
        newMessageDB.insertNewMessage(newMessageDomain);
    }

    private void initView() {
        userName = (TextView) findViewById(R.id.detail_user);
        type = (ImageView) findViewById(R.id.detail_type);
        profe = (TextView) findViewById(R.id.detail_profe);
        time = (TextView) findViewById(R.id.detail_time);
        head = (ImageView) findViewById(R.id.detail_head);
        title = (TextView) findViewById(R.id.detail_titile);
        content = (TextView) findViewById(R.id.detail_content);
        LinearLayout liuyan = (LinearLayout)findViewById( R.id.details_liuyan);
        iv_collect = (ImageView) findViewById(R.id.iv_collect);

        userName.setText(info.getUserName());
        profe.setText(info.getMajor());
        time.setText(info.getTime());
        title.setText(info.getTitle());
        content.setText(info.getContent());

        Bitmap bitmap = FileUtiles.getSmallBitmap(info.getHeadUrl());
        head.setImageBitmap(bitmap);

        if (info.getType().equals("在校生"))
            type.setImageResource(R.drawable.inschool);
        else
            type.setImageResource(R.drawable.outschool);

        content.setOnClickListener(new MyOnClickListener(0));
        liuyan.setOnClickListener(new MyOnClickListener(1));
        iv_collect.setOnClickListener(new MyOnClickListener(2));

        //内容太多可滚动查看
        content.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    /**
     * 控件的监听事件
     */
    private class MyOnClickListener implements View.OnClickListener {

        private int index = 0;
        public MyOnClickListener(int i) {
            index = i;
        }
        public void onClick(View v) {
            if(index == 0){
                Bundle bundle = new Bundle();
                bundle.putSerializable("shareContent", info);
                Intent intent = new Intent(ShareDetailsActivity.this,MingPian.class);
                intent.putExtras(bundle);
                startActivity(intent);

            }else if(index == 1){
                inputTitleDialog();
            }else if(index == 2){
                collect();
            }
        }
    }
    //收藏
    private void collect(){
        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");

        String dateStr = "";
        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            dateStr = simpleDateFormat.format(date);
        } catch (Exception e) {

        }

        collectionDB = new CollectionDB(this);
        collectionDB.open();

        CollectionDomain collection = new CollectionDomain();
        collection.setRelationId(info.get_id());
        collection.setUserName(account);
        collection.setTime(dateStr);
        collectionDB.insertCollection(collection);
    }

    //弹出留言框
    private void inputTitleDialog() {

        final EditText inputServer = new EditText(this);
        inputServer.setFocusable(true);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("留言").setIcon(
                R.drawable.icon).setView(inputServer).setNegativeButton("取消", null);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String inputName = inputServer.getText().toString();
                        leaveWord(inputName);

                        shareMessageDB = new ShareMessageDB(ShareDetailsActivity.this);
                        shareMessageDB.open();
                        message_list.clear();
                        message_list.addAll(shareMessageDB.queryByShareCoentenId(info.get_id()));
                        shareAdapter.setList(message_list);
                        shareAdapter.notifyDataSetChanged();
                    }
                });
        builder.show();
    }
}